﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aspose.Cells;
using TranslateJHR.Models;
using System.IO;

namespace TranslateJHR.Controllers
{
    public class HomeController : Controller
    {
        public static string path { get; set; }
        TranslateHelp help = new TranslateHelp();

        public ActionResult Index()
        {
            return View();
        }

        //1.遍历上传语言清单
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file, string path)
        {
            HomeController.path = path;
            var isExcel = help.InitializationExcel(file);
            if (TranslateHelp.translateModel == null || TranslateHelp.translateModel.Count == 0)
            {
                return View(false);
            }
            else
            {
                return View(true);
            }

        }

        //2.根据path去遍历服务器文件夹下的所有文件
        public JsonResult TraversalFile()
        {
            TranslateHelp.fileModels = null;
            TranslateHelp.fileModels = new List<FileModel>();

            DirectoryInfo dir = new DirectoryInfo(@path);
            help.TraversalFile(dir);
            if (TranslateHelp.fileModels == null || TranslateHelp.fileModels.Count == 0)
            {
                return Json(new
                {
                    Result = false,
                    Massage = "未找到(*.cshtnl)(*.js)文件"
                });
            }
            return Json(new
            {
                Result = true,
                Massage = "目录遍历完毕，找到所有的(*.cshtnl)(*.js)文件"
            }, JsonRequestBehavior.AllowGet);
        }

        //3.找到文件替换为英文
        //4.替换后字符串添加后缀写入文件（如果存在就覆盖）
        public JsonResult loopFile()
        {
            help.loopFile();
            //生成csproj文件
            //help.writeCsproj(@"C:\Users\jacak.yang\Desktop\X6.Site.csproj");
            return Json(new
            {
                Result = true,
                Massage = "生成“_en”文件完毕。"
            },JsonRequestBehavior.AllowGet);
        }

        public string ReturnExcelCount()
        {
            return TranslateHelp.translateModel.Count.ToString();
        }

        public string ReturnFileModels()
        {
            return TranslateHelp.fileModels.Count.ToString();
        }

        public string ReturnSucessCount()
        {
            return TranslateHelp.successCount.ToString();
        }
    }
}